import 'dart:typed_data';

import 'jm_usb_platform_interface.dart';
import 'model/model.dart';

export 'model/model.dart';

class JmUsb {
  // Future<String?> getPlatformVersion() {
  //   return JmUsbPlatform.instance.getPlatformVersion();
  // }
  static final JmUsbPlatform _platform = JmUsbPlatform.instance;

  static Stream<bool> get connectStream => _platform.connectStream;

  static Stream<Uint8List> get dataReceivedStream =>
      _platform.dataReceivedStream;

  static Stream<int> get dataLengthStream => _platform.dataLengthStream;

  static Future<void> init() async {
    await _platform.init();
  }

  static Future<void> closeDevice() async {
    await _platform.closeDevice();
  }

  static Future<bool> quickConnectDevice(
    UsbDevice usbDevice, {
    int configurationIndex = 0,
    int interfaceIndex = 0,
    int? endpointInIndex,
    int? endpointOutIndex,
  }) async {
    return _platform.quickConnectDevice(
      usbDevice,
      configurationIndex: configurationIndex,
      interfaceIndex: interfaceIndex,
      endpointInIndex: endpointInIndex,
      endpointOutIndex: endpointOutIndex,
    );
  }

  static Future<bool> quickConnectAccessory() async {
    return _platform.quickConnectAccessory();
  }

  static Future<int> quickGetFileDescriptor(
    UsbDevice usbDevice, {
    int configurationIndex = 0,
    int interfaceIndex = 0,
    int? endpointInIndex,
    int? endpointOutIndex,
  }) async {
    return _platform.quickGetFileDescriptor(usbDevice,
        configurationIndex: configurationIndex,
        interfaceIndex: interfaceIndex,
        endpointInIndex: endpointInIndex,
        endpointOutIndex: endpointOutIndex);
  }

  static Future<List<UsbDevice>> getDeviceList() async {
    return _platform.getDeviceList();
  }

  static Future<List<UsbAccessory>> getAccessoryList() async {
    return _platform.getAccessoryList();
  }

  static Future<bool> openDevice(UsbDevice usbDevice) async {
    return _platform.openDevice(usbDevice);
  }

  static Future<bool> hasPermission(UsbDevice usbDevice) async {
    return await _platform.hasPermission(usbDevice);
  }

  static Future<bool> requestPermission(UsbDevice usbDevice) async {
    return await _platform.requestPermission(usbDevice);
  }

  static Future<String> getSerialNumber() async {
    return await _platform.getSerialNumber();
  }

  static Future<int> getFileDescriptor() async {
    return await _platform.getFileDescriptor();
  }

  static Future<UsbConfiguration> getConfiguration(int index) async {
    return await _platform.getConfiguration(index);
  }

  static Future<bool> controlTransfer({
    int requestType = 0,
    int request = 0,
    int value = 0,
    int index = 0,
    Uint8List? data,
  }) async {
    return await _platform.controlTransfer(
      requestType: requestType,
      request: request,
      value: value,
      index: index,
      data: data ?? Uint8List(0),
    );
  }

  static Future<bool> writeData(Uint8List data, {int timeOut = 1000}) async {
    return _platform.writeData(data, timeOut);
  }

  static Future<Uint8List?> readData(int maxLength,
      {int timeOut = 1000}) async {
    return _platform.readData(maxLength, timeOut);
  }

  static Future<bool> accessoryWriteData(Uint8List data,
      {int timeOut = 1000}) async {
    return _platform.accessoryWriteData(data, timeOut);
  }

  static Future<Uint8List?> accessoryReadData(int maxLength,
      {int timeOut = 1000}) async {
    return _platform.accessoryReadData(maxLength, timeOut);
  }

  static Future<void> startDataListening({
    int type = 0,
    int accumulatedBufferSize = 0,
    int requestCount = 8,
    int bufferSize = 16384,
  }) async {
    await _platform.startDataListening(
      type: type,
      accumulatedBufferSize: accumulatedBufferSize,
      requestCount: requestCount,
      bufferSize: bufferSize,
    );
  }

  static Future<void> stopDataListening() async {
    await _platform.stopDataListening();
  }
}
